home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Vertigo / RenderEngine / DrawingUtils.h < prev    next >
Text File  |  2000-06-23  |  7KB  |  237 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    File:        DrawingUtils.h
  4. //
  5. //    Project:    MacHack 2000 - Vertigo!
  6. //    Authors:    Darrin Cardani, Drew Thaler, Ed Wynne
  7. //
  8. //    Date:        06/23/2000 (written entirely during the conference!)
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11.  
  12. #ifndef _H_DrawingUtils
  13. #define _H_DrawingUtils
  14.  
  15. #include <LowMem.h>
  16. #include <Menus.h>
  17. #include <Windows.h>
  18. #include <Quickdraw.h>
  19. #include <QDOffscreen.h>
  20.  
  21.  
  22. // RegionList class, for managing lists of regions
  23. class RegionList
  24. {
  25. public:
  26.     RegionList();
  27.     ~RegionList();
  28.     
  29.     void    Add(RgnHandle region, int duplicateRgn);
  30.     void    Remove(RgnHandle region, int dispose);
  31.     void    RemoveAll(int dispose);
  32.     
  33.     inline int    GetCount() const { return mItemsInArray; };
  34.     RgnHandle&    operator[] (UInt32 index);
  35.  
  36. protected:
  37.     RgnHandle    *mArray;
  38.     RgnHandle    mNull;
  39.     Handle        mArrayStorage;
  40.     UInt32        mItemsInArray;
  41.     UInt32        mCurrentArrayCapacity;
  42.  
  43. private:
  44.     RegionList(const RegionList&)    {}
  45. };
  46.  
  47.  
  48. class StRegion
  49. {
  50. public:
  51.     StRegion()                        { mRegion = NewRgn(); }
  52.     StRegion(const StRegion &inRgn)    { mRegion = NewRgn(); CopyRgn(inRgn.mRegion,mRegion); }
  53.     StRegion(RgnHandle inRgn)        { mRegion = NewRgn(); CopyRgn(inRgn,mRegion); }
  54.     StRegion(const Rect &inRect)    { mRegion = NewRgn(); RectRgn(mRegion,&inRect); }
  55.     ~StRegion()                        { if (mRegion != NULL) DisposeRgn(mRegion); mRegion = NULL; }
  56.     
  57.     StRegion&        operator = (const StRegion &inRgn)    { if (this != &inRgn) CopyRgn(inRgn.mRegion,mRegion); return *this; }
  58.     StRegion&        operator = (RgnHandle inRgn)        { if (inRgn == NULL) SetEmptyRgn(mRegion); else CopyRgn(inRgn,mRegion); return *this; }
  59.     StRegion&        operator = (const Rect    &inRect)    { RectRgn(mRegion,&inRect); return *this; }
  60.     
  61.     operator RgnHandle() const                            { return mRegion; }
  62.     operator Handle() const                                { return (Handle)mRegion; }
  63.     
  64.     bool IsEmpty() const                                { return EmptyRgn(mRegion); }
  65.     Rect &Bounds() const                                { return (**mRegion).rgnBBox; }
  66.     
  67.     StRegion&        operator += (RgnHandle inRegion)            { UnionRgn(mRegion, inRegion, mRegion); return *this; }
  68.     StRegion&        operator -= (RgnHandle inRegion)            { DiffRgn(mRegion, inRegion, mRegion); return *this; }
  69.     StRegion&        operator &= (RgnHandle inRegion)            { SectRgn(mRegion, inRegion, mRegion); return *this; }
  70.     StRegion&        operator |= (RgnHandle inRegion)            { MacUnionRgn(mRegion, inRegion, mRegion); return *this; }
  71.     StRegion&        operator ^= (RgnHandle inRegion)            { MacXorRgn(mRegion, inRegion, mRegion); return *this; }
  72.     bool            operator == (RgnHandle inRegion) const        { return EqualRgn(mRegion, inRegion); }
  73.     bool            operator != (RgnHandle inRegion) const        { return !EqualRgn(mRegion, inRegion); }
  74.     
  75.     StRegion&        operator += (const Rect    &inRect)            { StRegion rgn(inRect); UnionRgn(mRegion,rgn,mRegion); return *this; }
  76.     StRegion&        operator -= (const Rect    &inRect)            { StRegion rgn(inRect); DiffRgn(mRegion,rgn,mRegion); return *this; }
  77.     StRegion&        operator &= (const Rect    &inRect)            { StRegion rgn(inRect); SectRgn(mRegion,rgn,mRegion); return *this; }
  78.     StRegion&        operator |= (const Rect    &inRect)            { StRegion rgn(inRect); UnionRgn(mRegion,rgn,mRegion); return *this; }
  79.     StRegion&        operator ^= (const Rect    &inRect)            { StRegion rgn(inRect); XorRgn(mRegion,rgn,mRegion); return *this; }
  80.     bool            operator == (const Rect    &inRect) const        { StRegion rgn(inRect); return EqualRgn(mRegion,rgn); }
  81.     bool            operator != (const Rect    &inRect) const        { StRegion rgn(inRect); return !EqualRgn(mRegion,rgn); }
  82.     
  83. protected:
  84.     RgnHandle    mRegion;
  85. };
  86.  
  87.  
  88. class StSetGWorld
  89. {
  90. public:
  91.     StSetGWorld(CGrafPtr port, GDHandle device = NULL)    { GetGWorld(&mOldPort,&mOldDevice); SetGWorld(port,device); }
  92.     ~StSetGWorld()                                        { SetGWorld(mOldPort,mOldDevice); }
  93.     
  94. protected:
  95.     CGrafPtr    mOldPort;
  96.     GDHandle    mOldDevice;
  97.  
  98. private:
  99.     StSetGWorld(const StSetGWorld&)    {}
  100. };
  101.  
  102.  
  103. class StSetPort
  104. {
  105. public:
  106.     StSetPort(GrafPtr port)                { GetPort(&mOldPort); SetPort(port); }
  107.     StSetPort(CGrafPtr port)            { GetPort(&mOldPort); SetPort((GrafPtr)port); }
  108.     ~StSetPort()                        { SetPort(mOldPort); }
  109.  
  110. protected:
  111.     GrafPtr        mOldPort;
  112.  
  113. private:
  114.     StSetPort(const StSetPort&) {}
  115. };
  116.  
  117.  
  118. class StClipRgnState
  119. {
  120. public:
  121.     StClipRgnState()                        { GetClip(mOldClip); mNewClip = mOldClip; }
  122.     StClipRgnState(const Rect &clipRect)    { GetClip(mOldClip); mNewClip = mOldClip; ClipTo(clipRect); }
  123.     StClipRgnState(RgnHandle clipRgn)        { GetClip(mOldClip); mNewClip = mOldClip; ClipTo(clipRgn); }
  124.     ~StClipRgnState()                        { SetClip(mOldClip); }
  125.     
  126.     void ClipTo(const Rect &inRect)            { mNewClip &= inRect; SetClip(mNewClip); }
  127.     void ClipTo(RgnHandle inRgn)            { mNewClip &= inRgn; SetClip(mNewClip); }
  128.     bool IsEmpty()                            { return mNewClip.IsEmpty(); }
  129.     void DontClip()                            { Rect wideOpen = {-32000,-32000,32000,32000}; mNewClip = wideOpen; SetClip(mNewClip); }
  130.     
  131. protected:
  132.     StRegion    mOldClip;
  133.     StRegion    mNewClip;
  134.     
  135. private:
  136.     StClipRgnState(const StClipRgnState&)    {}
  137. };
  138.  
  139.  
  140. class StGWorld
  141. {
  142. public:
  143.     StGWorld(const GrafPtr sameSizeAsThis)    { Create(sameSizeAsThis->portRect); }
  144.     StGWorld(const CGrafPtr sameSizeAsThis)    { Create(sameSizeAsThis->portRect); }
  145.     StGWorld(const StGWorld &gworld)        { Create(gworld.mGWorld->portRect); }
  146.     StGWorld(const Rect &inRect)            { Create(inRect); }
  147.     ~StGWorld()                                { if (mGWorld) DisposeGWorld(mGWorld); mGWorld = NULL; }
  148.     
  149.     operator GWorldPtr()    { return mGWorld; }
  150.     operator BitMap*()        { return &((GrafPtr)mGWorld)->portBits; }
  151.     
  152.     const Rect*                PortRect() { return &mGWorld->portRect; }
  153.     
  154. protected:
  155.     GWorldPtr    mGWorld;
  156.  
  157. private:
  158.     void Create(const Rect &inRect);
  159. };
  160.  
  161.  
  162. class StDisposeWindow
  163. {
  164. public:
  165.     StDisposeWindow(const WindowPtr window)        { mWindow = window; }
  166.     ~StDisposeWindow()                            { DisposeWindow(mWindow); }
  167.  
  168. protected:
  169.     WindowPtr        mWindow;
  170. };
  171.  
  172.  
  173. class StHideMenuBar
  174. {
  175. public:
  176.     StHideMenuBar()        {
  177.                             // Adjust the menu bar height
  178.                             mMenuBarHeight = GetMBarHeight();
  179.                             LMSetMBarHeight(0);
  180.                             
  181.                             // Re-add the menu bar to the gray rgn
  182.                             Rect    mbarRect = qd.screenBits.bounds;
  183.                             mbarRect.bottom = mMenuBarHeight;
  184.                             mbarRect.right = qd.screenBits.bounds.right;
  185.                             
  186.                             StRegion newGrayRgn = mOldGrayRgn = GetGrayRgn();
  187.                             newGrayRgn += mbarRect;
  188.                             CopyRgn(newGrayRgn,GetGrayRgn());
  189.                             
  190.                             // Redraw
  191.                             DrawMenuBar();
  192.                         }
  193.     
  194.     ~StHideMenuBar()    {
  195.                             CopyRgn(mOldGrayRgn,GetGrayRgn());
  196.                             LMSetMBarHeight(mMenuBarHeight);
  197.                             DrawMenuBar();
  198.                         }
  199.  
  200. protected:
  201.     short        mMenuBarHeight;
  202.     StRegion    mOldGrayRgn;
  203. };
  204.  
  205.  
  206.  
  207. inline void CopyGWorldToGWorld(const GWorldPtr inSrc, GWorldPtr inDst, RgnHandle maskRgn = NULL)
  208. {
  209.     StSetGWorld    setPort(inDst);
  210.     ForeColor(blackColor);
  211.     BackColor(whiteColor);
  212.     CopyBits(&((GrafPtr)inSrc)->portBits,
  213.              &((GrafPtr)inDst)->portBits,
  214.              &inSrc->portRect,
  215.              &inDst->portRect,
  216.              srcCopy, maskRgn);
  217. }
  218.  
  219. inline void CopyPortToGWorld(const GrafPtr inSrc, GWorldPtr inDst, RgnHandle maskRgn = NULL) { CopyGWorldToGWorld((GWorldPtr)inSrc,inDst,maskRgn); }
  220. inline void CopyPortToGWorld(const CGrafPtr inSrc, GWorldPtr inDst, RgnHandle maskRgn = NULL) { CopyGWorldToGWorld((GWorldPtr)inSrc,inDst,maskRgn); }
  221.  
  222. inline void CopyGWorldToPort(const GWorldPtr inSrc, GrafPtr inDst, RgnHandle maskRgn = NULL)
  223. {
  224.     StSetPort    setPort(inDst);
  225.     ForeColor(blackColor);
  226.     BackColor(whiteColor);
  227.     CopyBits(&((GrafPtr)inSrc)->portBits,
  228.              &((GrafPtr)inDst)->portBits,
  229.              &inSrc->portRect,
  230.              &inDst->portRect,
  231.              srcCopy, maskRgn);
  232. }
  233.  
  234.  
  235. #endif // _H_DrawingUtils
  236.  
  237.